package com.d2.pay.model;

import lombok.Data;


/**
 * Auth: Alexander Lo
 * Date: 2020-03-24
 * Description:
 */
@Data
public class D2SendItem {

//    struct order
//    {
//        char orderindex[10];
//        char name[21];
//        union
//        {
//            char ItemCode[4];
//            DWORD dwItemCode;
//        };
//        BYTE quality;
//        DWORD count;
//        BOOL done;
//    };

    private String orderIndex = "";
    private String name ="";


    private String itemCode;
    private byte quality;
    private int count;




    public byte[] toBytes(){
        byte[] data = new byte[40];

        //1
        byte[] orderIndex  = this.orderIndex.getBytes();
        if (orderIndex.length > 10)
            throw  new RuntimeException("order struct orderIndex eroor");
        System.arraycopy(orderIndex, 0, data, 0, orderIndex.length);


        //2
        byte[] name = this.name.getBytes();
        if (name.length >21)
            throw  new RuntimeException("order struct name eroor");

        System.arraycopy(name, 0, data, 10, name.length);



        byte[] itemCode = this.itemCode.getBytes();
        if (itemCode.length >4)
            throw  new RuntimeException("order struct name itemCode");

        System.arraycopy(itemCode, 0, data, 31, itemCode.length);



        data[35] = this.quality;


        data[36] = (byte) (this.count   & 0xff);
        data[37] = (byte) (this.count >> 8 & 0xff);
        data[38] = (byte) (this.count >> 16 & 0xff);
        data[39] = (byte) (this.count >> 24 & 0xff);






        return data;

    }


}
